# -*- coding: utf-8 -*-
# @Time    : 2019/8/5 11:38
# @Author  : xiao
from basic.data import data
from basic.device.Android import Android
from airtest.core.api import *

class IndexPage(Android):


    #搜索输入框
    searchBtm="com.cloudbae.lovenanning:id/search_bg"

    viewpager="com.cloudbae.lovenanning:id/in_viewpager"

    XYNNBtn="信用南宁"

    #首页
    # IndexBtn= "com.cloudbae.lovenanning:id/icon"
    IndexBtn="com.cloudbae.lovenanning:id/tab_tv1"
    # 底部登录提示
    loginMessage = "com.cloudbae.lovenanning:id/realNameFrame"

    #手机号输入框
    inputPhoneNum = "com.cloudbae.lovenanning:id/etInputPhoneNum"

    # 首页应用名称
    appName = "com.cloudbae.lovenanning:id/tvQrCodeTitle"

    # 升级弹框关闭
    closeUpdate = "com.cloudbae.lovenanning:id/ib_close"

    # 特殊应用展台
    showCase = "com.cloudbae.lovenanning:id/recyclerGridView"

    # 推荐应用展台
    recommendCase = "com.cloudbae.lovenanning:id/card_grid_recycle"

    # 我的收藏
    collectBtn = "我的收藏"

    # 首页banner展台
    bannderCase = "com.cloudbae.lovenanning:id/bannerViewPager"

    # 2列卡片展台
    cardCase = "com.cloudbae.lovenanning:id/app_title"

    #推荐收藏展台tab
    collectTab = "com.cloudbae.lovenanning:id/indicator"

    # @classmethod
    # def clickIndex(self):
    #     self.poco(self.IndexBtn)[1].click()
    #     print("点击首页")

    @classmethod
    def clickIndex(self):
        self.poco(self.IndexBtn).click()
        print("点击首页")

    @classmethod
    def clickSearch(self):
        # sleep(5)
        self.poco(self.searchBtm).wait(6).click();

        # poco(self.searchBtm).click();

    @classmethod
    def waitForIndexPage(cls):
        cls.poco(cls.searchBtm).wait(30)



    #判断是否是启动介绍页面，是则滑动
    @classmethod
    def isViewpager(cls):
        if cls.poco(cls.viewpager).wait(2).exists():
            for i in  range(1,4):
                swipe((800,800),(100,800),1)
                cls.poco(cls.viewpager).click()

    @classmethod
    def clickXYNNBtn(cls):
        cls.poco(cls.XYNNBtn)

    @classmethod
    def clickLoginMessage(self):
        inputPhoneNum = self.poco(self.inputPhoneNum)
        if inputPhoneNum.wait(3).exists():
            return inputPhoneNum
        else:
            self.poco(self.loginMessage).wait(5).click()

    @classmethod
    def getInputPhoneNum(self):
        inputPhoneNum = self.poco(self.inputPhoneNum).get_text()
        return inputPhoneNum

    @classmethod
    def getAppName(self):
        appName = self.poco(self.appName)
        listText=[]
        for i in range(0,len(appName)):
            listText.append(appName[i].get_text())
        print(listText)
        return listText

    @classmethod
    def ifUpdate(self):
        update = self.poco(self.closeUpdate)
        if update.wait(3).exists():
            update.click()

    @classmethod
    def spaShow(self):
        show = self.poco(self.showCase).child(type="android.widget.FrameLayout").child(type="android.widget.FrameLayout").offspring(self.appName)
        listAppName = []
        for i in range(0, len(show)):
            listAppName.append(show[i].get_text())
        print("特殊应用：", listAppName)
        return listAppName


    @classmethod
    def recommendShow(self):
        show = self.poco(self.recommendCase).child(type="android.widget.FrameLayout").child(
            type="android.widget.FrameLayout").offspring(self.appName)
        listAppName = []
        for i in range(0, len(show)):
            listAppName.append(show[i].get_text())
        print("推荐应用：", listAppName)
        return listAppName

    @classmethod
    def collectApp(self):
        print("点击我的收藏")
        self.poco(text="我的收藏").wait(3).click()
        show = self.poco(self.recommendCase).child(type="android.widget.FrameLayout").child(
            type="android.widget.FrameLayout").offspring(self.appName)
        listAppName = []
        for i in range(0, len(show)):
            listAppName.append(show[i].get_text())
        print("我的收藏应用：", listAppName)
        return listAppName

    @classmethod
    def bannderShow(self):
        show = self.poco(self.recommendCase)
        print("首页展台:", show)
        if show.exists():
            return True

    @classmethod
    def cardShow(self):

        swipe((613, 1102), (613, 10))
        sleep(3)
        list = self.poco(self.cardCase)
        listCardName = []
        for i in range(0, len(list)):
            # print(list[i].get_text())
            listCardName.append(list[i].get_text())
        print("2列卡片获取到的广告标题：", listCardName)
        num = len(list)
        if listCardName[(num-1)] is None:
            print("2列卡片最后一个卡片获取不到标题，疑似少配了一个广告")
            return False
        else:
            return True

    @classmethod
    def clickAdd(cls,btnText):
        cls.poco(text=btnText).parent().click()
        print("点击收藏添加按钮")
        sleep(5)

    # 判断推荐收藏展台tab的相对位置是否在屏幕中间往上，不是则向上滑动
    @classmethod
    def getCollectTabPos(cls):
        pos = cls.poco(cls.collectTab).get_position()
        print(pos)
        if pos[1] > 0.5:
            cls.poco(cls.collectTab).swipe([0, -0.25])
            print("首页上滑")
